﻿using System;
using Abp;
using Abp.Authorization;
using Abp.Dependency;
using Hicap.AuthorizationServer.Core;
using Hicap.AuthorizationServer.Core.Common;

namespace Hicap.AuthorizationServer.Authorization
{
    public class LoginResultTypeHelper : AbpServiceBase, ITransientDependency
    {
        public LoginResultTypeHelper()
        {
            LocalizationSourceName = AuthorizationServerConsts.LocalizationSourceName;
        }

        public Exception CreateExceptionForFailedLoginAttempt(LoginResultType result, string userCode)
        {
            switch (result)
            {
                case LoginResultType.Success:
                    return new Exception("Don't call this method with a success result!");

                case LoginResultType.InvalidUserCode:
                case LoginResultType.InvalidPassword:
                    return new AbpAuthorizationException(L("LoginFailed") + L("InvalidUserCodeOrPassword"));

                case LoginResultType.UserIsNotActive:
                    return new AbpAuthorizationException(L("LoginFailed") + L("UserIsNotActiveAndCanNotLogin", userCode));

                case LoginResultType.LockedOut:
                    return new AbpAuthorizationException(L("LoginFailed") + L("UserLockedOutAuthorizationServersage"));

                default: // Can not fall to default actually. But other result types can be added in the future and we may forget to handle it
                    Logger.Warn("Unhandled login fail reason: " + result);
                    return new AbpAuthorizationException(L("LoginFailed"));
            }
        }
    }
}